<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>驾校后台管理系统</title>
    <meta name="description" content="驾校后台管理系统">
    <meta name="keywords" content="驾校后台管理系统">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>预约管理<small>教练管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>预约管理</el-breadcrumb-item>
            <el-breadcrumb-item>教练管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="教练编码/姓名/教练车号" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage(1)" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="code" label="教练编码" align="center"></el-table-column>
                <el-table-column prop="name" label="教练名称" align="center"></el-table-column>
                <el-table-column label="性别" align="center">
                    <template slot-scope="scope">
                        <span>{{scope.row.sex == '1' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="carCode" label="教练车编号" align="center"></el-table-column>
                <el-table-column prop="subject" label="教授科目" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page.sync="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增教练" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="编码">
                                                <el-input v-model="formData.code"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="姓名">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.sex">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="教练车号">
                                                <el-input v-model="formData.carCode"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="教授科目">
                                                <el-input v-model="formData.subject" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="注意事项">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="学员信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>学员编码</th>
                                            <th>学员名称</th>
                                            <th>培训科目</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="studentIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.code}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.subject}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑教练" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="编码">
                                                <el-input v-model="formData.code"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.sex">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="教练车号">
                                                <el-input v-model="formData.carCode"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="教授科目">
                                                <el-input v-model="formData.subject" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="注意事项">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="学员信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>学员编码</th>
                                            <th>学员名称</th>
                                            <th>培训科目</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="studentIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.code}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.subject}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    const vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的学员列表数据
            studentIds: [],//新增和编辑表单中学员对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false//控制编辑窗口显示/隐藏
        },
        created() {//页面初始化后加载
            //获取储存的当前分页
            this.pagination.currentPage = this.getContextData("currentPage") || 1;
            this.findPage();
        },
        methods: {
            //给sessionStorage存值
            setContextData: function (key, value) {
                if (typeof value == "string") {
                    sessionStorage.setItem(key, value);
                } else {
                    sessionStorage.setItem(key, JSON.stringify(value));
                }
            },
            // 从sessionStorage取值
            getContextData: function (key) {
                const str = sessionStorage.getItem(key);
                if (typeof str == "string") {
                    try {
                        return JSON.parse(str);
                    } catch (e) {
                        return str;
                    }
                }
                return;
            },
            //编辑
            handleEdit() {
                //发送Ajax请求提交学员 Id集合 和 表单数据
                axios.post("/coach/edit.do?studentIds="+this.studentIds,this.formData).then((response)=>{
                    //关闭编辑窗口
                    this.dialogFormVisible4Edit = false;
                    if (response.data.flag){
                        this.$message({
                            message: response.data.message,
                            type:'success'
                        });
                    }else {
                        this.$message.error(response.data.message);
                    }
                }).finally(()=>{
                    //调用分页查询重新加载页面
                    this.findPage();
                })
            },
            //添加
            handleAdd() {
                //发送ajax请求将模型数据提交到后台处理
                axios.post("/coach/add.do?studentIds=" + this.studentIds, this.formData).then((res) => {
                    //隐藏新增窗口
                    this.dialogFormVisible = false;
                    if (res.data.flag) {
                        //新增成功，弹出成功提示
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    } else {
                        //新增失败，弹出错误提示
                        this.$message.error(res.data.message);
                    }
                    //不管操作成功与否都应该再次分页查询
                }).finally(() => {
                    this.findPage();
                });
            },
            //分页查询
            findPage(page) {
                if (page === 1) {
                    this.pagination.currentPage = 1;
                }

                //封装分页参数
                const param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                //发送ajax请求，提交分页相关参数
                axios.post("/coach/findPage.do", param).then((res) => {
                    //为VUE对象的模型数据赋值，基于双向数据绑定展示到页面
                    this.dataList = res.data.rows;//当前页展示的数据集合
                    this.pagination.total = res.data.total;//总数据量
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },
            // 弹出添加窗口
            handleCreate() {
                //重置表单
                this.resetForm();
                //弹出新增窗口
                this.dialogFormVisible = true;
                this.activeName = 'first';//默认选中first对应的tab
                this.studentIds = []; //重置选中的学员
                //发送ajax请求，查询所有的学员信息，转为json，赋值给tableData
                axios.get("/student/findAll.do").then((res) => {
                    if (res.data.flag) {
                        //查询学员数据成功
                        this.tableData = res.data.data;
                    } else {
                        //查询学员数据失败
                        this.$message.error(res.data.message);
                    }
                });
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                //alert(row.id);
                //1.发送Ajax请求根据id查询教练信息，用于基本信息的回显
                axios.get("/coach/findById.do?id="+row.id).then((res)=>{
                   if (res.data.flag){
                       //返回消息为true
                       this.dialogFormVisible4Edit = true;//弹出编辑窗口
                       //默认选中第一个基本信息栏
                       this.activeName = 'first';
                       //为模型数据赋值，基于vue数据双向绑定进行信息的回显
                       this.formData = res.data.data;
                       //2.发送请求查询所有学员信息，用于学员窗口展示
                       axios.get("/student/findAll.do").then((res)=>{
                           if (res.data.flag){
                               //为tableData赋值
                               this.tableData = res.data.data;
                               //3.发送请求查询教练组中所有的学员id,由于信息的回显
                               axios.get("/coach/findStudentIdByCoachId.do?id="+row.id).then((res)=>{
                                   if (res.data.flag){
                                       //为模型数据赋值
                                       this.studentIds = res.data.data;
                                   }else {
                                       this.$message.error(res.data.message);
                                   }
                               });
                           }else {
                               this.$message.error(res.data.message);
                           }
                       });
                   }else {
                        this.$message.error("获取信息失败，请刷新当前页面");
                   }
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //设置最新的页码
                this.pagination.currentPage = currentPage;
                //储存当前页码
                this.setContextData("currentPage", this.pagination.currentPage);
                //重新调用findPage方法进行分页查询
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                //弹出消息提示框
                this.$confirm('确认删除当前教练记录吗',{type:'warning'}).then(()=>{
                    //alert("确定")
                    //发送Ajax请求根据id删除信息
                    axios.post("/coach/deleteById.do?id="+row.id).then((res)=>{
                        if (res.data.flag){
                            //删除成功
                            this.$message({
                                message:res.data.message,
                                type:'success'
                            });
                        }else {
                            //删除失败
                            this.$message.error(res.data.message);
                        }
                    }).finally(()=>{
                        //刷新页面
                        this.findPage();
                    });
                });
            }
        }
    });
</script>
</html>
